package com.stox.core.ui.swing.widget;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Rectangle;

import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.UIManager;

import com.stox.core.ui.swing.util.SwingUtil;
import com.stox.resources.image.ImageIconLoader;

public class MainFrame  extends JFrame{
	private static final long serialVersionUID = 1L;

	private static MainFrame instance;
	
	private final MainDesktop desktop = new MainDesktop();
	private final MessagePanel messagePanel = new MessagePanel();
	private final MenuBar menuBar = new MenuBar();

	public static MainFrame getInstance(){
		if(instance == null){
			instance = new MainFrame();
		}
		return instance;
	}
	
	private MainFrame() {
		super("Stockz");
		if(instance != null){
			throw new UnsupportedOperationException();
		}
		
		instance = this;
	}
	
	public void construct(){
		setLookAndFeel();
		setSize();
		desktop.setDragMode(JDesktopPane.OUTLINE_DRAG_MODE);
		setJMenuBar(menuBar);
		add(desktop,BorderLayout.CENTER);
		add(messagePanel, BorderLayout.SOUTH);
		
		/*List<UiConfigurer> uiComponents = Container.getImplementations(UiConfigurer.class);
		if(uiComponents != null && !uiComponents.isEmpty()){
			Collections.sort(uiComponents, OrderComparator.instance);
			for(UiConfigurer uiComponent : uiComponents){
				uiComponent.configure();
			}
		}*/
		
	}
	
	private void setLookAndFeel(){
		setIconImage(ImageIconLoader.loadImage("icon.jpg"));
		setLookAndFeel(0, new String[]{"javax.swing.plaf.nimbus.NimbusLookAndFeel",
		"com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel"});
	}
	
	private void setLookAndFeel(int pointer, String[] lafs){
		try{
			if(pointer < lafs.length){
				UIManager.setLookAndFeel(lafs[pointer]);
			}
		}catch(Exception cnfe){
			setLookAndFeel(++pointer, lafs);
		}
	}
	
	
	private void setSize(){
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		Rectangle maxBounds = SwingUtil.getScreenSize();
        setMaximizedBounds(maxBounds);
        Dimension maxDimension = new Dimension(maxBounds.width/3,maxBounds.height/3);
        setMaximumSize(maxDimension);
        setExtendedState(JFrame.MAXIMIZED_BOTH);
		setPreferredSize(maxDimension);
	}

	public MessagePanel getMessagePanel(){
		return messagePanel;
	}
	
	public MainDesktop getDesktop() {
		return desktop;
	}
}
